package com.desmart.renjiao.membersearch;

import android.util.Log;

import com.desmart.renjiao.bean.GetMemberResult;
import com.desmart.renjiao.xmlparase.ResultXmlUtils;
import com.google.gson.Gson;
import com.lemon.library.net.rx.RxObservableListener;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class MemberSearchP extends MemberSearch.MemberSearchP{

    @Override
    void memberSearch(String keyword) {

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\">\n" +
                "<soapenv:Header/>\n" +
                "<soapenv:Body>\n" +
                "<ser:Seachinfo>\n" +
                "<arg0>" + keyword +
                "</arg0>\n" +
                "</ser:Seachinfo>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>\n";


        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.memberSearch(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                String ss = null;

                try {
                    ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    Log.e("Hqq",result);
                    GetMemberResult getMemberResult = new Gson().fromJson(result, GetMemberResult.class);
                    if ("200".equals(getMemberResult.getCode())){
                        mView.onMemberList(getMemberResult.getUsers());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
